package edu.calpoly.lab7;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SMSActivity extends Activity {
	
	private static final String SMS_SENT = "SMS_SENT";
	private static final String SMS_DELIVERED = "SMS_DELIVERED";
	
	private PendingIntent pendingSent = null;
	private PendingIntent pendingDelivered = null;
	
	private MessageSentReceiver messageSentReceiver = null;
	private MessageDeliveredReceiver messageDeliveredReceiver = null;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms);
        ((Button)findViewById(R.id.sendButton)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SMSActivity.this.sendSMS();
				SMSActivity.this.resetUI();
			}
        });
        ((Button)findViewById(R.id.resetButton)).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SMSActivity.this.cancelStatusUpdates();
				SMSActivity.this.resetUI();
			}
        });
        initStatusReceivers();
    }
	
	private void initStatusReceivers() {
		this.messageSentReceiver = new MessageSentReceiver();
		this.messageDeliveredReceiver = new MessageDeliveredReceiver();
		this.registerReceiver(messageSentReceiver, new IntentFilter(SMS_SENT));
		this.registerReceiver(messageDeliveredReceiver, new IntentFilter(SMS_DELIVERED));
	}
	
	private void sendSMS() {
		SmsManager smsManager = SmsManager.getDefault();
		
		Intent sentIntent = new Intent(SMS_SENT);
		Intent deliveredIntent = new Intent(SMS_DELIVERED);
	    pendingSent = PendingIntent.getBroadcast(this, 0, sentIntent, 0);
	    pendingDelivered = PendingIntent.getBroadcast(this, 0, deliveredIntent, 0);
	    
		String number = (String)((TextView)this.findViewById(R.id.messageNumber)).getText().toString();
		String text = (String)((TextView)this.findViewById(R.id.messageText)).getText().toString();
		smsManager.sendTextMessage(number, null, text, pendingSent, pendingDelivered);
		
	}
	
	private void cancelStatusUpdates() {
		if(this.messageSentReceiver != null)
			this.unregisterReceiver(this.messageSentReceiver);
		if(this.messageDeliveredReceiver != null)
			this.unregisterReceiver(this.messageDeliveredReceiver);
	}
	
	private void resetUI() {
    	((TextView)findViewById(R.id.statusText)).setText(R.string.unsent);
    	((EditText)findViewById(R.id.messageNumber)).setText("");
    	((EditText)findViewById(R.id.messageText)).setText("");
	}
	
	private class MessageSentReceiver extends BroadcastReceiver {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			Activity activity = (Activity)context;
			TextView tv = (TextView)activity.findViewById(R.id.statusText);
			if(this.getResultCode() == Activity.RESULT_OK)
				tv.setText(R.string.sent);
			else
				tv.setText(R.string.error);
		}
	}
	
	private class MessageDeliveredReceiver extends BroadcastReceiver {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			Activity activity = (Activity)context;
			TextView tv = (TextView)activity.findViewById(R.id.statusText);
			if(this.getResultCode() == Activity.RESULT_OK)
				tv.setText(R.string.delivered);
			else
				tv.setText(R.string.error);
		}
	}

}
